package ee.tlnk.dao;

import java.sql.Date;
import java.sql.Time;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import ee.tlnk.model.OrderModel;

/**
 * The Class OrderDAOImpl.
 */
@Repository
public class OrderDAOImpl implements OrderDAO {

	/** The session factory. */
	@Autowired
	private SessionFactory sessionFactory;

	/* (non-Javadoc)
	 * @see ee.tlnk.dao.OrderDAO#addOrder(ee.tlnk.model.OrderModel)
	 */
	public void addOrder(OrderModel order) {
		sessionFactory.getCurrentSession().save(order);
	}

	/* (non-Javadoc)
	 * @see ee.tlnk.dao.OrderDAO#listOrder()
	 */
	@SuppressWarnings("unchecked")
	public List<OrderModel> listOrder() {

		return sessionFactory.getCurrentSession().createQuery("from OrderModel")
			.list();
	}

	/**
	 * List order time.
	 *
	 * @param day the day
	 * @param timeStart the time start
	 * @param timeEnd the time end
	 * @return the list
	 */
	@SuppressWarnings("unchecked")
	public List<OrderModel> listOrderTime(Date day, Time timeStart, Time timeEnd) {

		return sessionFactory.getCurrentSession().createQuery("from OrderModel where day = ? ")
			.list();
	}
	
	
	/* (non-Javadoc)
	 * @see ee.tlnk.dao.OrderDAO#removeOrder(java.lang.Integer)
	 */
	public void removeOrder(Integer id) {
		OrderModel order = (OrderModel) sessionFactory.getCurrentSession().load(
				OrderModel.class, id);
		if (null != order) {
			sessionFactory.getCurrentSession().delete(order);
		}

	}
}
